package com.taxi.driver.module.order.price;

import android.app.Activity;

import com.alibaba.fastjson.JSON;
import com.amap.api.location.AMapLocation;
import com.amap.api.maps.model.LatLng;
import com.qianxx.utils.RxUtil;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.config.BizStatus;
import com.taxi.driver.config.PositionType;
import com.taxi.driver.data.amap.AMapManager;
import com.taxi.driver.data.config.ConfigRepository;
import com.taxi.driver.data.order.OrderRepository;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.socket.message.UploadLocationMessage;
import com.taxi.driver.socket.message.base.MessageType;
import com.taxi.driver.util.SpeechUtil;

import javax.inject.Inject;

/**
 * 功能描述：
 */
public class PriceCheckPresenter extends BasePresenter implements PriceCheckContract.Presenter {

    private final PriceCheckContract.View mView;
    private final OrderRepository mOrderRepository;
    private final UserRepository mUserRepository;
    private final ConfigRepository mConfigRepository;
    private final AMapManager mAMapManager;
    @Inject
    AMapManager amapManager;
    private String mOrderUuid;

    @Inject
    PriceCheckPresenter(PriceCheckContract.View view, OrderRepository orderRepository, UserRepository userRepository,
                        ConfigRepository configRepository, AMapManager aMapManager, AMapManager mapManager) {
        mView = view;
        mOrderRepository = orderRepository;
        amapManager=mapManager;
        mUserRepository = userRepository;
        mConfigRepository = configRepository;
        mAMapManager = aMapManager;
    }

    @Override
    public void subscribe() {
        super.subscribe();
        reqFareItems(); //获取费用明细
    }

    @Override
    public void setOrderUuid(String orderUuid) {
        mOrderUuid = orderUuid;
    }

    @Override
    public String getOrderUuid() {
        return mOrderUuid;
    }

    @Override
    public void reqFareItems() {
        mSubscriptions.add(
                mOrderRepository.orderFare(mOrderUuid)
                        .compose(RxUtil.applySchedulers())
                        .subscribe(mView::setDisplay,
                                ex -> showNetworkError(ex, R.string.network_error, mView, mUserRepository)));
    }

    @Override
    public void confirmFare(Integer highwayFare, Integer roadBridgeFare, Integer otherFare) {
        LatLng latLng = new LatLng(mAMapManager.getLastLocation().getLatitude(), mAMapManager.getLastLocation().getLongitude());
        String uploadText = uploadText(BizStatus.EMPTY_DRIVING, latLng, PositionType.SJQR);

        mSubscriptions.add(
                mOrderRepository.confirmFare(mOrderUuid, highwayFare, roadBridgeFare, otherFare, uploadText)
                        .compose(RxUtil.applySchedulers())
                        .doOnNext(s -> SpeechUtil.speech((Activity) mView, "确认费用，已发送给乘客。乘客再见，欢迎再次乘坐！"))
                        .subscribe(s -> mView.confirmFareSuccess(),
                                ex -> {
                                    mView.resetBtnDisplay();
                                    PriceCheckPresenter.this.showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                                }));
    }

    private String uploadText(int bizStatus, LatLng latLng, String positionType) {
        AMapLocation lastLocation = mAMapManager.getLastLocation();
        UploadLocationMessage uploadLocationMessage = new UploadLocationMessage();
        uploadLocationMessage.setAppid(((Activity) mView).getString(R.string.yueyue_appkey));
        uploadLocationMessage.setBizStatus(bizStatus);
        uploadLocationMessage.setDriverType(mUserRepository.getDriverType());
        uploadLocationMessage.setOrderUuid(mOrderUuid);
        uploadLocationMessage.setAreaCode(lastLocation.getAdCode());
        uploadLocationMessage.setLat(latLng.latitude);
        uploadLocationMessage.setLng(latLng.longitude);
        uploadLocationMessage.setPositionType(positionType);
        uploadLocationMessage.setDistance(0d);
        uploadLocationMessage.setAngle(lastLocation.getBearing());
        uploadLocationMessage.setSpeed(lastLocation.getSpeed());
        uploadLocationMessage.setElevation(lastLocation.getAltitude());
        uploadLocationMessage.setAccuracy(lastLocation.getAccuracy());
        uploadLocationMessage.setCarLevelType(mUserRepository.getCarLevelType());
        uploadLocationMessage.setClientUuid(mUserRepository.getLocalDriverUuid());
        uploadLocationMessage.setDriverUuid(mUserRepository.getLocalDriverUuid());
        uploadLocationMessage.setLocationUuid(System.currentTimeMillis() + "");
        uploadLocationMessage.setType(MessageType.UPLOAD_LOCATION);
        return JSON.toJSONString(uploadLocationMessage);
    }

    @Override
    public String getPriceRule() {
        return mConfigRepository.getPriceRules();
    }

/*    @Override
    public void getCurrentCity() {
        LatLng mCurrentLatLng = new LatLng(amapManager.getLastLocation().getLatitude(), amapManager.getLastLocation().getLongitude());

        amapManager.geocodeSearch(mCurrentLatLng)
                .compose(RxUtil.applySchedulers())
                .subscribe(address -> {
                    mView.getCurrentCity(address.getCity());

                }, ex -> showNetworkError(ex, R.string.error, mView));
    }*/
}
